/**
*菜单渲染 横条形菜单
*/

var menuContainer = "<div id=\"menu-box\" class=\"\"></div>";
var menuBox = '<div id="indexMenuBox" class="col-sm-7 col-sm-offset-2 infobox-container"></div>';

function initMenu() {
	try {
		createMenu();
	} catch (e) {
		alert('菜单加载出错...');
	}
}

function addBox(){
	$("#menu-box-container").append(menuContainer);
}

function getBox() {
	var $box = $("#menu-box");
	if(!$box || $box.length <= 0){
		addBox();
		$box = $("#menu-box");
	}
	return $box;
}

function getMenuBox() {
	return $("#indexMenuBox");
}

function createMenu() {
	getBox().html(menuBox);
	var $box = getMenuBox();
	if(!fnArray || fnArray.length <= 0){
		alert("菜单加载失败,菜单列表fnArray无效");
		return false;
	}
	for (var i = 0; i < fnArray.length; i++) {
		var menu = fnArray[i];
		if (!menu['data']['id']) {
			menu['data']['id'] = (fnArray[i]['data']['id'] = 'idx-menu-' + i);
		}

		$.each(menu['data'], function(key, val) {
			menu['menu'] = (fnArray[i]['menu'] = menu['menu'].replace('${' +key + '}',val));
		});
		
		$box.append(menu['menu']);
		if(menu['click']){
			$("#" + menu['data']['id']).on('click',menu['click']);
		}
	}
}

initMenu();